package com.stx.action;

import java.text.SimpleDateFormat;
import java.util.Set;

import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.stx.entity.User;
import com.stx.entity.User_collection;
import com.stx.service.UserService;
import com.stx.service.impl.UserServiceImpl;
import com.stx.util.MD5_Test;

public class UserAction extends ActionSupport {

	private UserService us;
	private User user = null;
	private String date;
    private String email;
    private String password;
    private Set<User_collection> user_collection_set;
    
    
    
    
	public Set<User_collection> getUser_collection_set() {
		return user_collection_set;
	}

	public void setUser_collection_set(Set<User_collection> user_collection_set) {
		this.user_collection_set = user_collection_set;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

	public UserService getUs() {
		return us;
	}

	public void setUs(UserService us) {
		this.us = us;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	/*
	 * 得到用户的所有信息到用户管理中心
	 * 修改了显示的时间格式
	 * email换成了一个变量，检测是否email是否更新了
	 * 判断img是否为空
	 * */
	public String findUser() {
		System.out.println("执行开始。。。。");
		user=(User) ServletActionContext.getRequest().getSession().getAttribute("user");
		System.out.println("执行结束。。。。");
		if(user!=null){
	         SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	        date=sdf.format(user.getUs_createtime());
	        email=user.getUs_email();
	        if(user.getUs_image()==null){
	        	user.setUs_image("images/user_normal.jpg");
	        }
	        //得到收藏信息
	        user_collection_set=user.getUs_collection();
		return "success";
		}else{
			addActionError("你没有权限或权限丢失!请登录或注册");
			return "failure";
		}
	}
	/*
	 * 更新用户的操作
	 * 首先判断下是否用户修改了email
	 * 如果更换邮箱先检查下是否被使用
	 * */
	public String upadteUser(){
		System.out.println(ServletActionContext.getRequest().getParameter("email"));
		System.out.println(email);
		User u=(User) ServletActionContext.getRequest().getSession().getAttribute("user");
		/*检查下session是否有效*/
		if(u!=null){
		user.setUs_password(u.getUs_password());
		user.setUs_email(email);
		user.setUs_id(u.getUs_id());
		}else{
			addActionError("请重新登陆！");
			return "sessionerror";
		}
		/*判断是否邮箱被修改了*/
		if(!email.equals(u.getUs_email())&&(us.getUser(user).size()!=0)){
				addActionError("此邮箱已被使用了");
				return "failure";
			}else{
				user.setUs_password(MD5_Test.MD5(user.getUs_password()));
				us.updateUser(user);
				addActionError("修改成功");
				/*
				 * 重新得打user然胡重置session
				 * */
			    user=us.findUser(user).get(0);
				ServletActionContext.getRequest().getSession().setAttribute("user", user);
				return "success";
			}
	}
	/*
	 * 修改用户的密码
	 * 首先检查原始密码是否正确
	 * */
	public String updatePassword(){
		
		User u=(User) ServletActionContext.getRequest().getSession().getAttribute("user");
		/*检查下session是否有效*/
		if(u!=null){
		user.setUs_nickname(u.getUs_nickname());
		user.setUs_email(email);
		user.setUs_image(u.getUs_image());
		user.setUs_id(u.getUs_id());
		}else{
			addActionError("请重新登陆！");
			return "sessionerror";
		}
		/*比对下密码是否输入有误*/
		password=MD5_Test.MD5(password);
		if(!u.getUs_password().equals(password)){
			addActionError("密码输入错误");
			return  "failure";
		}else{
			user.setUs_password(MD5_Test.MD5(user.getUs_password()));
			us.updateUser(user);
			addActionError("密码修改成功");
			user=us.findUser(user).get(0);
			ServletActionContext.getRequest().getSession().setAttribute("user", user);
			return "success";
		}
	}
}
